package com.gexiaobao.pigeon.ui.auction

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseActivity
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.ext.initFooter
import com.gexiaobao.pigeon.app.ext.loadListData
import com.gexiaobao.pigeon.app.ext.loadServiceInit
import com.gexiaobao.pigeon.app.util.StatusBarUtil
import com.gexiaobao.pigeon.app.util.Util
import com.gexiaobao.pigeon.app.weight.recycleview.SpaceItemDecoration
import com.gexiaobao.pigeon.databinding.ActivityBiddingHistoryBinding
import com.gexiaobao.pigeon.ui.adapter.AdapterBiddingHistory
import com.gexiaobao.pigeon.viewmodel.AuctionViewModel
import com.kingja.loadsir.core.LoadService
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat

/**
 * @Author      : hxw
 * @Date        : 2024/11/12 10:26
 * @Describe    : 竞拍历史
 */
class ActivityBiddingHistory : BaseActivity<AuctionViewModel, ActivityBiddingHistoryBinding>() {

    private val adapter: AdapterBiddingHistory by lazy { AdapterBiddingHistory(arrayListOf()) }
    private var mGoodId = 0
    private lateinit var loadsir: LoadService<Any>

    @SuppressLint("NotifyDataSetChanged")
    override fun initView(savedInstanceState: Bundle?) {
        StatusBarUtil.setTranslucentForImageView(this, 0, null)
        mDatabind.includeBar.toolBarLayout.setBackgroundColor(ContextCompat.getColor(this, R.color.color_99CCFF))
        mDatabind.includeBar.tvTitle.text = "竞拍历史"

        mGoodId = intent.getIntExtra("goodId", 0)
        loadsir = loadServiceInit(mDatabind.includeList.swipeRecyclerView) {
            //点击重试时触发的操作
            onRefresh(isRefresh = true)
        }
        onRefresh(isRefresh = true)

        mDatabind.includeList.swipeRecyclerView.init(LinearLayoutManager(this), adapter).let {
            it.addItemDecoration(SpaceItemDecoration(0, Util.dp2px(this, 8f)))
            it.initFooter {
                onRefresh(isRefresh = false)
            }
        }


        mDatabind.includeList.swipeRefreshLayout.init {
            onRefresh(isRefresh = true)
        }
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.includeBar.ivBack) {
            when (it) {
                mDatabind.includeBar.ivBack -> finish()
            }
        }
    }

    private fun onRefresh(isRefresh: Boolean) {
        showLoading(getString(R.string.loading))
        mViewModel.getAppPriceHistory(isRefresh, mGoodId.toString())
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun createObserver() {
        super.createObserver()
        /** 获取竞价历史 */
        mViewModel.appPriceHistoryResult.observe(this) {
            loadListData(it, adapter, loadsir, mDatabind.includeList.swipeRecyclerView, mDatabind.includeList.swipeRefreshLayout)
        }
    }

}